package com.tedu.redisdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

@Configuration

public class RedisConfig {
    @Bean//将方法返回的对象交给spring容器管理
    public RedisTemplate<String, Serializable> initRedisTemplate(
        RedisConnectionFactory redisConnectionFactory){
        //初始化Redis的模板类
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        //为模板类设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //key序列化为字符串格式
        redisTemplate.setKeySerializer(RedisSerializer.string());
        //value序列化为json格式
        redisTemplate.setValueSerializer(RedisSerializer.json());
        //hash散列value中的key和value
        redisTemplate.setHashKeySerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());

        return redisTemplate;
    }


}
